# -*- coding: utf-8 -*-
"""
Created on Mon Nov 18 13:50:53 2019

@author: XCL01
"""

from sklearn.datasets import load_digits
from sklearn.linear_model.logistic import LogisticRegression
from sklearn.metrics import accuracy_score


#加载数据
digits, dig_label = load_digits(return_X_y=True)

#确定训练数和测试数
N = len(digits)
Ntrain = int(N*0.8)
Ntest = N - Ntrain

# 切分
x_train = digits[: Ntrain, :]
y_train = dig_label[: Ntrain]
x_test  = digits[Ntrain: , :]
y_test  = dig_label[Ntrain: ]

#建立逻辑回归模型
logic = LogisticRegression()
logic.fit(x_train,y_train)
be_train = logic.predict(x_train)
be_test  = logic.predict(x_test)

#计算训练模型和测试模型得准确性
acc_train = accuracy_score(y_train, be_train)
acc_test = accuracy_score(y_test, be_test)
print("accuracy train = %f, accuracy_test = %f" % (acc_train, acc_test))
score_train = logic.score(x_train, y_train)
score_test  = logic.score(x_test, y_test)
print("score_train = %f, score_test = %f" % (score_train, score_test))
